package com.cplh.sharding.demo.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @author: Jenkin.K
 * @since: 2020/12/9
 * @version: 1.0
 * @description:
 **/
@Data
public class ResultDto<T> implements Serializable {

    private static final long serialVersionUID = 7002335400410293052L;

    private int code;

    private String message;

    private T data;

    public static <T> ResultDto<T> success() {
        ResultDto<T> response = new ResultDto<>();
        response.setCode(MessageCodeEnum.SUCCESS.getCode());
        response.setMessage(MessageCodeEnum.SUCCESS.getMessage());
        return response;
    }

    public static <T> ResultDto<T> success(T result) {
        ResultDto<T> response = new ResultDto<>();
        response.setCode(MessageCodeEnum.SUCCESS.getCode());
        response.setMessage(MessageCodeEnum.SUCCESS.getMessage());
        response.setData(result);
        return response;
    }

    public static <T> ResultDto<T> failure(int code, String msg) {
        ResultDto<T> response = new ResultDto<>();
        response.setCode(code);
        response.setMessage(msg);
        return response;
    }

    public static <T> ResultDto<T> response(MessageCodeEnum messageEnum, T result) {
        ResultDto<T> response = response(messageEnum);
        response.setData(result);
        return response;
    }

    public static <T> ResultDto<T> paramFailure(String msg) {
        ResultDto<T> response = new ResultDto<>();
        response.setCode(MessageCodeEnum.PARAM_ERROR.getCode());
        response.setMessage(msg);
        return response;
    }

    public boolean isSuccess() {
        return code == MessageCodeEnum.SUCCESS.getCode();
    }

    public static <T> ResultDto<T> response(MessageCodeEnum messageEnum) {
        ResultDto<T> response = new ResultDto<>();
        response.setCode(messageEnum.getCode());
        response.setMessage(messageEnum.getMessage());
        return response;
    }

}
